/* ---------------------------------------------------------------------------------------------------------------------
 * File: teamlineupscreen.cpp
 * Author: Daniel Pop, Felix Rodriguez
 * Purpose: The scope for this class is to give the user the option to choose the possition of the players
 * ---------------------------------------------------------------------------------------------------------------------*/
#include "teamlineupscreen.h"
#include "ui_teamlineupscreen.h"
#include "base.h"
#include "playericon.h"
#include "stdio.h"
#include "stdlib.h"
#include <QRectF>
#include <QGraphicsItem>
#include <QList>

teamLineUpScreen::teamLineUpScreen(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::teamLineUpScreen)
{
    ui->setupUi(this);

}
teamLineUpScreen::teamLineUpScreen(QVector<Player *> home, QVector<Player *> away):
    QMainWindow(0),
    ui(new Ui::teamLineUpScreen)
{
    ui->setupUi(this);
    isHome = true;
    icons = new QVector<PlayerIcon*>();
    homePlayers= home;
    awayPlayers= away;
    theScene2 = new QGraphicsScene();

    int width = ui->graphicsView->geometry().width();
    int height = ui->graphicsView->geometry().height();
    theScene2->setSceneRect(0, 0, width,height);

    // turn off the scrollbar in graphicsview
    ui->graphicsView->setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
    ui->graphicsView->setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);

    // Create a rectangle for the positions of the player on the field usign x and y coordinates.
    catcher=new Base();
    catcher->setBountdingRect(new QRectF(-75,-75,100,100));
    catcher->setX(370); catcher->setY(600);
    theScene2->addItem(catcher);
    first_Base=new Base();
    first_Base->setBountdingRect(new QRectF(-75,-75,100,100));
    first_Base->setX(560); first_Base->setY(440);
    theScene2->addItem(first_Base);
    second_Base=new Base();
    second_Base->setBountdingRect(new QRectF(-75,-75,100,100));
    second_Base->setX(490); second_Base->setY(310);
    theScene2->addItem(second_Base);
    third_Base=new Base();
    third_Base->setBountdingRect(new QRectF(-75,-75,100,100));
    third_Base->setX(210); third_Base->setY(430);
    theScene2->addItem(third_Base);
    short_Stop =new Base();
    short_Stop->setBountdingRect(new QRectF(-75,-75,100,100));
    short_Stop->setX(290); short_Stop->setY(310);
    theScene2->addItem(short_Stop);
    center_Field=new Base();
    center_Field->setBountdingRect(new QRectF(-75,-75,100,100));
    center_Field->setX(380); center_Field->setY(130);
    theScene2->addItem(center_Field);
    left_Field=new Base();
    left_Field->setBountdingRect(new QRectF(-75,-75,100,100));
    left_Field->setX(180); left_Field->setY(270);
    theScene2->addItem(left_Field);
    right_Field=new Base();
    right_Field->setBountdingRect(new QRectF(-75,-75,100,100));
    right_Field->setX(600); right_Field->setY(270);
    theScene2->addItem(right_Field);
    pitcher=new Base();
    pitcher->setBountdingRect(new QRectF(-75,-75,100,100));
    pitcher->setX(390); pitcher->setY(450);
    theScene2->addItem(pitcher);

    showHomePlayers();
    // Tell myGraphicsView that this is my scene
    ui->graphicsView->setScene(theScene2);
    ui->graphicsView->setViewportUpdateMode(QGraphicsView::FullViewportUpdate);



}

teamLineUpScreen::~teamLineUpScreen()
{
    delete ui;
}

// display a list of players below the field using player icons for the home team
void teamLineUpScreen::showHomePlayers(){
    for(int i=0; i<homePlayers.size(); i++){
        PlayerIcon *player_i = new PlayerIcon(homePlayers.at(i));
        player_i->setBountdingRect(new QRectF(-75,-75,100,100));
        if(i<5){
            player_i->setX(30+i*100);
            player_i->setY(760);

        }else{
            player_i->setX(30+(i-5)*100);
            player_i->setY(860);
        }

        theScene2->addItem((player_i));
        icons->append(player_i);
    }
}
// display a list of players below the field using player icons for the away team
void teamLineUpScreen::showAwayPlayers(){
    for(int i=0; i<awayPlayers.size(); i++){
        PlayerIcon *player_i = new PlayerIcon(awayPlayers.at(i));
        player_i->setBountdingRect(new QRectF(-75,-75,100,100));
        if(i<5){
            player_i->setX(30+i*100);
            player_i->setY(760);

        }else{
            player_i->setX(30+(i-5)*100);
            player_i->setY(860);
        }

        theScene2->addItem((player_i));

    }
}

Player* teamLineUpScreen::getHomePitcher(){
    return homePitcher;
}
Player* teamLineUpScreen::getAwayPitcher(){
    return awayPitcher;
}
// saves the player placed on the pitcher base as the pitcher.
void teamLineUpScreen::save(){
    if(isHome==true){
        QList<QGraphicsItem*> collidingItems = pitcher->collidingItems();
        if(collidingItems.size()>0){
            PlayerIcon* temp = (PlayerIcon*)collidingItems.at(0);
            homePitcher = temp->getPlayer();
        for(int i=0; i<icons->size();i++){
            icons->at(i)->~QGraphicsItem();
            }
        icons->clear();
        }else
        {
            ui->prompt->setText("Please select a pitcher");
        }
        isHome= false;
        showAwayPlayers();
    }
    else{
        QList<QGraphicsItem*> collidingItems = pitcher->collidingItems();
        if(collidingItems.size()>0){
            PlayerIcon* temp = (PlayerIcon*)collidingItems.at(0);
            awayPitcher = temp->getPlayer();
        for(int i=0; i<icons->size();i++){
            icons->at(i)->~QGraphicsItem();
            }
        icons->clear();
        }else
        {
            ui->prompt->setText("Please select a pitcher");
        }
        int hp= getPlayerLineupPosition(homePitcher);
        int ap= getPlayerLineupPosition(awayPitcher);

        ScoreKeeperScreen* screen = new ScoreKeeperScreen(homePlayers,hp, awayPlayers, ap );
        screen->show();
        this->hide();
    }
}

int teamLineUpScreen::getPlayerLineupPosition(Player *p){
    for(int i=0; i<homePlayers.size(); i++){
        if(p == homePlayers.at(i)){
            return i;
        }
    }
    for(int i=0; i<awayPlayers.size(); i++){
        if(p == awayPlayers.at(i)){
            return i;
        }
    }
    return -1;
}
